<?php

/**
 * Config
 *
 * Provides access to all of the various warlock config options and general
 * info.
 *
 * Warlock is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 *
 * @package    warlock
 * @author     David Cole <neophyte@sourcetutor.com>
 * @license    GNU/GPL v2
 */

// Object dependencies
using('Library.DataTypes.Array.Conversion');
using('Library.Patterns.Registry');

class Config extends Registry
{
    /**#@+
     * Required Core Objects
     *
     * These are references to the core objects required by this
     * class.
     *
     * @access private
     * @var    object
     */
    var $_Error;
    var $_Profiler;
    /**#@-*/

    /**
     * Path to Config File
     *
     * @var string
     * @access private
     */
    var $_ConfigFile;

    /**
     * Config File Loaded
     *
     * @var bool
     * @access private
     */
    var $_Loaded    = false;

    /**
     * Config File Writable
     *
     * @var bool
     * @access private
     */
    var $_Writable  = false;

    /**
     * Constructor
     *
     * General initialisation, try and load the config file
     * and set some properties.
     *
     * @return Config
     */
    function __construct()
    {
        // Add self to Vault
        Vault::Add('Config', &$this);

        // Call the registry constructor
        parent::__construct();

        // Grab references to required objects
        $this->_Error = &Vault::Get('Debug_Error');
        $this->_Profiler = &Vault::Get('Debug_Profiling');

        /*
         * Attempt to load the main warlock
         * config file
         */

        $t = $this->_Profiler->Start('Load Config Data');
        $this->_ConfigFile = W_ENV_PATH.'/config.inc.php';



        if (file_exists($this->_ConfigFile))
        {
            include $this->_ConfigFile;
            $this->_registry = $settings;
            $this->_Loaded   = true;
            $this->_Writable = is_writable($this->_ConfigFile)?true:false;
        }
        else
            $this->_Loaded = false;

        $this->_Profiler->Stop($t);
    }

    /**
     * Save Config File
     *
     * @return bool
     * @access public
     */
    function Save()
    {
        if ($this->_Loaded && $this->_Writable)
        {
            if (($fp = fopen($this->_ConfigFile, 'w')) === false)
            {
                $this->_Error->Log('Config :: Unable to open config file \''.$this->_ConfigFile.'\'', W_LOG_ERR);
                return false;
            }
            else
            {
                if (!fwrite($fp, array2php($this->_registry, 'settings')))
                {
                    $this->_Error->Log('Config :: Error writing to file \''.$this->_ConfigFile.'\'', W_LOG_ERR);
                    return false;
                }
                else
                    return true;
            }
        }
        else
            return false;
    }

    /**
     * Config File Loaded
     *
     * Primarily used by the index.php controller in determining
     * which application to load
     *
     * @return bool
     * @access public
     */
    function IsLoaded()
    {
        return $this->_Loaded;
    }

    /**
     * Config File Writable
     *
     * Primarily used by the Installation application in determining
     * how to handle the config file
     *
     * @return unknown
     */
    function IsWritable()
    {
        return $this->_Writable;
    }
}

?>